package top.itmeans.yes.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.itmeans.yes.entity.Menu;
import top.itmeans.yes.entity.Role;
import top.itmeans.yes.entity.RoleMenuRelation;
import top.itmeans.yes.entity.RoleResourceRelation;
import top.itmeans.yes.mapper.RoleMapper;
import top.itmeans.yes.mapper.RoleMenuRelationMapper;
import top.itmeans.yes.mapper.RoleResourceRelationMapper;
import top.itmeans.yes.service.RoleService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-05-05
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuRelationMapper roleMenuRelationMapper;
    @Resource
    private RoleResourceRelationMapper roleResourceRelationMapper;

    @Override
    public int create(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setAdminCount(0);
        role.setSort(0);
        return roleMapper.insert(role);
    }

    @Override
    public int update(Long id, Role role) {
        return roleMapper.update(role,
                new LambdaQueryWrapper<Role>().eq(Role::getId,id)
        );
    }

    @Override
    public int delete(List<Long> ids) {
        return roleMapper.deleteBatchIds(ids);
    }

    @Override
    public Page<Role> list(String keyword, Integer pageSize, Integer pageNum) {
        Page<Role> rolePage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Role> roleQueryWrapper = new LambdaQueryWrapper<>();
        roleQueryWrapper.like(!StrUtil.isEmpty(keyword),Role::getName,keyword);
        return roleMapper.selectPage(rolePage,roleQueryWrapper);
    }

    @Override
    public List<Menu> getMenuList(Long adminId) {
        return roleMapper.getMenuList(adminId);
    }

    @Override
    public List<Menu> listMenu(Long roleId) {
        return roleMapper.getMenuListByRoleId(roleId);
    }

    @Override
    public List<top.itmeans.yes.entity.Resource> listResource(Long roleId) {
        return roleMapper.getResourceListByRoleId(roleId);
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        LambdaQueryWrapper<RoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenuRelation::getRoleId,roleId);
        roleMenuRelationMapper.delete(queryWrapper);
        //批量插入新关系
        for (Long menuId : menuIds) {
            RoleMenuRelation relation = new RoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuRelationMapper.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        LambdaQueryWrapper<RoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleResourceRelation::getRoleId,roleId);
        roleResourceRelationMapper.delete(queryWrapper);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            RoleResourceRelation relation = new RoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceRelationMapper.insert(relation);
        }
        return resourceIds.size();
    }
}
